<?php
/**
 * Created by PhpStorm.
 * User: xing
 * Date: 2018/11/2
 * Time: 1:03 PM
 */

namespace app\wechat\controller;

use taobao\AliOss;

class Tools extends Common
{

    /**
     * 返回所有字典常量
     * @return \think\response\Json
     */
    public function getDictionaryParam()
    {

        return successReturn([
            "h5_url" => config("constants.xdH5Url")
        ]);

    }

    /**
     * 获取oss上传凭证
     * @return \think\response\Json
     */
    public function getOssToken()
    {

        $ossToken = getRedisOssToken();
        if (!$ossToken) {
            $ossToken = getOssToken();
            setRedisOssToken($ossToken);
        } else {
            $ossToken = json_decode($ossToken, true);
        }

        return successReturn($ossToken['Credentials']);

    }

    /**
     * 获取小程序上传图片凭证
     * @return \think\response\Json
     */
    public function getMiniProgramOssToken() {

        $returnData = getMiniProgramOssToken();

        return successReturn($returnData);

    }

    /**
     * 上传图片
     * @return \think\response\Json
     */
    public function uploadImage()
    {

        $tempFile = $_FILES['file']['tmp_name'];
        $fileName = md5(uniqid(mt_rand(), true)) . ".png";
        $info = AliOss::uploadFile($tempFile, $fileName);

        $url = $info['info']['url'];
        $start = strrpos($url, "/");
        $imgName = substr($url, $start);

        $returnData['info']['url'] = config('oss.Cname') . $imgName;

        return successReturn($returnData);

    }

    /**
     * 获取小程序二维码
     */
    public function getWxaCode()
    {

        header('content-type:image/jpeg');

        $page = $this->request->get("page");
        $scene = $this->request->get("scene");

        $image = getWxaCode($scene, $page);

        echo $image;
        exit();

    }

    /**
     * 放入新formId
     */
    public function pushNewFormId()
    {

        $currentCustomer = $this->request->currentCustomer;
        $formId = $this->request->post("formId");

        pushCustomerFormId($currentCustomer["id"], $formId);

        return successReturn();

    }

}